package com.manage.action;

import com.base.service.ApplicationService;
import com.framework.base.controller.BaseViewController;
import com.framework.util.StringUtil;
import com.framework.view.KxdParams;
import com.framework.view.ResultDatas;
import com.framework.view.UserInfoView;
import com.manage.service.ChinaAreaService;
import com.manage.service.OrgService;
import com.model.TChina;
import com.model.TSysOrg;
import com.model.TSysRole;
import com.modeldiy.TChinaNode;
import com.utils.TreeUtiles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;

/**
 *
 */
@Controller
@RequestMapping("/soa/china")
public class ChinaAreaController extends BaseViewController {
    @Resource(name = "chinaAreaServiceImpl")
    ChinaAreaService service;

    @Resource(name = "orgServiceImpl")
    OrgService orgService;

    @Resource(name = "applicationServiceImpl")
    ApplicationService applicationService;

    /**
     * 跳转到首页
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "index", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView query (HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String,Object> params = new HashMap<String,Object>();
        UserInfoView<TSysRole> userInfo = getSessionUser(request);
        params.put("user", userInfo);
        return new ModelAndView("/goods/goodsType/goodsTypeIndex", params);
    }

    /**
     * 查询所有的商品类型
     *
     * @param request
     * @param response
     * @param params
     * @param start
     * @param length
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "datas/search", method = { RequestMethod.POST })
    @ResponseBody
    public Object conditionsPaging (HttpServletRequest request, HttpServletResponse response, KxdParams params, Long start, Long length) throws IOException {
        ResultDatas resultDatas = new ResultDatas();
        List<TChina> list = service.findAll();
        if (list.size() > 0) {
            resultDatas.setSuccess(0);
            resultDatas.setMsg("拿到数据");
            resultDatas.setData(list);
        } else {
            resultDatas.setSuccess(1);
            resultDatas.setData("");
            resultDatas.setMsg("没有数据");
        }
        list = null;
        return resultDatas;

    }

    @RequestMapping(value = "datas/goodsTypeBox", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object goodsTypeBox (HttpServletRequest request, HttpServletResponse response) throws IOException {

        List<TChina> list = null;
        try {
            list = service.findAll();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("==获取下拉失败==");
        }
        return list;
    }

    /**
     * 通过ID查找商品类型
     *
     * @param request
     * @param response
     * @param id
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "datas/find", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public TChina find (HttpServletRequest request, HttpServletResponse response, Long id) throws IOException {
        TChina model = null;
        if (id != null) {
            model = service.findById(id);
        }
        return model;
    }


    /**
     * 新增方法(主要做页面跳转到模态框取值)
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "inst", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView add (HttpServletRequest request, HttpServletResponse response) {

        return editGoodsType(request, response, null);//撞到编辑页面
    }

    /**
     * 修改方法
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @RequestMapping(value = "edit", method = { RequestMethod.POST, RequestMethod.GET })
    public ModelAndView editGoodsType (HttpServletRequest request, HttpServletResponse response, Long id) {
        TChina model = null;
        if (id != null) {
            model = service.findById(id);
        } else {
            model = new TChina();
            String pid = request.getParameter("pid");
            if (StringUtil.isEmpty(pid)) {
                pid = "0";
            }
            model.setPid(Long.valueOf(pid));
            model.setIsDeleted(0);
        }

        return new ModelAndView("/goods/goodsType/goodsTypeEdit", "model", model);
    }

    /**
     * 保存对象，inst和edit最后都要走到这里
     *
     * @param request
     * @param response
     * @param model
     * @return
     * @throws IOException
     * @throws ParseException
     */
    @RequestMapping(value = "datas/save", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object save (HttpServletRequest request, HttpServletResponse response, TChina model) throws IOException, ParseException {
        UserInfoView userInfo = this.getSessionUser(request);
        Map<String,Object> result = new HashMap<String,Object>();
        if (model.getId() != null) {
//            TGoodsType goodsType = goodsTypeService.findById(model.getId());
//            goodsType.setbUnitId(model.getbUnitId());
//            goodsType.setlUnitId(model.getlUnitId());
//            goodsType.setIsDeleted(model.getIsDeleted());
//            goodsType.setName(model.getName());
//            goodsType.setPid(model.getPid());
//            goodsType.setProperties(model.getProperties());
//            goodsTypeService.saveOrupdate(goodsType);
            model = null;
//            goodsType=null;
        } else {
//            goodsTypeService.saveOrupdate(model);
            result.put("model", model);
            result.put("msg", "新增成功");
            result.put("status", 0);
            model = null;
        }
        System.out.println(result);
        return result;
    }


    @RequestMapping(value = "datas/del", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object del (HttpServletRequest request, HttpServletResponse response, Long id) throws IOException {
        Map<String,Object> result = new HashMap<String,Object>();
        try {
            service.del(id);
            result.put("msg", "删除成功");
            result.put("status", 0);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("msg", "删除失败");
            result.put("status", 1);
        }
        return result;
    }

    @RequestMapping(value = "findTree", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object goodsUp (HttpServletRequest request, HttpServletResponse response, Long id) throws IOException {
        UserInfoView userInfo = this.getSessionUser(request);
        ResultDatas resultDatas = new ResultDatas();

        List myAreas = applicationService.getMyAreas(userInfo.getOrgId());
        if (myAreas.size() > 0) {
            resultDatas.setSuccess(0);
            resultDatas.setData(myAreas);
        } else {
            resultDatas.setSuccess(1);
            resultDatas.setData("");
            resultDatas.setMsg("没有数据");
        }
        myAreas = null;

        return resultDatas;
    }

    @RequestMapping(value = "areaNodes", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public Object areaNodes (HttpServletRequest request, HttpServletResponse response) throws IOException {
        UserInfoView userInfo = this.getSessionUser(request);
        ResultDatas resultDatas = new ResultDatas();

        TSysOrg org = applicationService.findOrg(userInfo.getOrgId());
        List<TChina> myAreas = applicationService.getAllSon(org.getDataAuthor(), true);
        List<TChinaNode> list =new ArrayList<TChinaNode>();
        if(org.getId().intValue()==1){
            TChinaNode chinaNode = new TChinaNode();
            chinaNode.setId(0L);
            chinaNode.setpId(0L);
            chinaNode.setIsDeleted(0);
            chinaNode.setName("全国");
            list.add(chinaNode);
        }
        for (TChina area : myAreas){
            TChinaNode chinaNode = new TChinaNode(area);
            list.add(chinaNode);
        }

        if (list.size() > 0) {
            resultDatas.setSuccess(0);
            resultDatas.setData(list);
        } else {
            resultDatas.setSuccess(1);
            resultDatas.setData("");
            resultDatas.setMsg("没有数据");
        }
        myAreas = null;

        return resultDatas;
    }

    @RequestMapping(value = "allChina", method = { RequestMethod.POST, RequestMethod.GET })
    @ResponseBody
    public ModelAndView allChina (HttpServletRequest request, HttpServletResponse response, Long parentOrgId) throws IOException {
        Map<String,Object> params = new HashMap<String,Object>();
        List<TChina> allChina = new ArrayList<TChina>();
        if (parentOrgId == 1) {
            allChina = service.findByParentId(0L);
        } else {
            TSysOrg parentOrg = orgService.find(parentOrgId);
            //获取父级权限范围
            Long chinaId = parentOrg.getDataAuthor();
            allChina = service.findByParentId(chinaId);
        }
        params.put("allChina", allChina);
        return new ModelAndView("/org/allCity", params);
    }
}
